﻿using IDEAMS.core.opm;
using System.Collections.Generic;
using System.Linq;

namespace IDEAMS.model
{
    public class SpectrumPkgBuffer
    {
        static int PKG_NO_MATCH_ERROR = -2;
        static int SUB_PKG_NO_MATCH_ERROR = -3;

        public int channelNo;
        public int pkgNo;
        private int nSubPkg;
        private Dictionary<int, SpectrumAcqData> spSubPkgDic = new Dictionary<int, SpectrumAcqData>();

        public SpectrumPkgBuffer()
        {
            pkgNo = -1;
            nSubPkg = -1;
        }

        public void Clear()
        {
            pkgNo = -1;
            nSubPkg = -1;
            spSubPkgDic.Clear();
        }

        public bool isEmpty()
        {
            return spSubPkgDic.Count() == 0 ? true : false;
        }

        public bool isFull()
        {
            return (spSubPkgDic.Count() == nSubPkg) ? true : false;
        }

        public int Add(SpectrumAcqData spData)
        {
            if (!isEmpty() && spData.pkgNo != pkgNo)
            {
                return PKG_NO_MATCH_ERROR;
            }

            //不应该出现的错误，如果出现了，应该是服务端的设置错误，在一个包中不可能出现相同的分包号
            if (spSubPkgDic.ContainsKey(spData.subPkgNo))
            {
                return SUB_PKG_NO_MATCH_ERROR;
            }

            if (isEmpty())
            {
                this.channelNo = spData.channelNo;
                this.pkgNo = spData.pkgNo;
                this.nSubPkg = spData.nSubPkg;
            }

            spSubPkgDic.Add(spData.subPkgNo, spData);

            return 0;
        }

        public void Remove(int subPkgNo)
        {
            spSubPkgDic.Remove(subPkgNo);
        }


        public List<SpectrumAcqData> GetData()
        {
            return this.spSubPkgDic.Values.ToList();
        }

    }
}

